package net.xdclass.forum.util;

import org.apache.commons.dbcp2.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Description
 * @Author Huzng
 * @Remark 数据库连接池工具类
 * @Version 1.0
 **/
public class DataSourceUtil {

    private static DataSource dataSource = null;

    static {
        try {

            InputStream inputStream = DataSourceUtil.class.getClassLoader().getResourceAsStream("database.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            dataSource = BasicDataSourceFactory.createDataSource(properties);


        } catch (Exception e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError("初始化DBPC失败");

        }
    }
    /**
     * 获取连接池
     * @return dataSource
     */
    public static DataSource getDataSource(){
        try {

            InputStream inputStream = DataSourceUtil.class.getClassLoader().getResourceAsStream("database.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError("初始化DBPC失败");

        }
        return dataSource;
    }
}
